/* 
 * File:   Bonus.cpp
 * Author: paula
 * 
 * Created on 19 de noviembre de 2014, 12:35
 */

#include "Bonus.h"
#include "Object.h"
#include "ObjFactory.h"

Bonus::Bonus() {

}

Bonus::Bonus(ObjFactory *objFactory, int id, float x, float y) {

    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);
    this->id = id;
    switch (id) {
        case BONUS_FLASH:
            this->width = BONUS_FLASH_WIDTH;
            this->height = BONUS_FLASH_HEIGHT;
            break;
        case BONUS_LIFE:
            this->width = BONUS_LIFE_WIDTH;
            this->height = BONUS_LIFE_HEIGHT;
            break;
    }
    Dot *dot = new Dot(x, y, this->width, this->height);
    //    this->dot = dot;    
    Object *object = objFactory->getBonus(dot->dotPixelsToWorld(), dot->sizeDotPixelsToWorld(), id);
    this->object = object;
    myLogFile->writeInLog(0, "[0] Se ha creado un bonus exitosamente\n");

    this->id = id;

    this->data.objectId = BONUS;
    this->data.imageId = id;
    this->data.estatico = false;
    this->data.x = x;
    this->data.y = y;
    this->data.height = this->height;
    this->data.width = this->width;
    this->data.rot = 0;

    this->data.idCharacter = VACIO;
    this->data.stateCharacter = VACIO;

    this->isEqual = true;
    this->hasDiedOrRevived = true;
    this->dorsp = 0;
    this->isUsed = false;
    this->timeToDisappear = 5; //seconds
    delete dot;
}

Bonus::Bonus(const Bonus & orig) {
}

void Bonus::restartPlayer() {
}

void Bonus::changePosition(float x, float y) {
    Dot *dot = new Dot(x, y);
    dot->dotPixelsToWorld();
    b2Vec2* pos = dot->getPos();
    this->object->getBody()->SetTransform(*pos, 0);
    delete dot;
    this->hasDiedOrRevived = true;
}

void Bonus::updatePosition() {
    b2Vec2 pos = this->object->getPosition();
    this->data.x = pos.x;
    this->data.y = pos.y;
}

struct ObjectInit Bonus::getData() {
    this->updateData();
    this->data.level = 0;
    return this->data;
}

void Bonus::updateData() {
    this->updatePosition();
}

void Bonus::kill() {
    this->data.imageId = -1;
    this->hasDiedOrRevived = true;
    this->changePosition(-100, -100);
    this->isUsed = false;
}

void Bonus::revive(float posX, float posY) {
    this->changePosition(posX, posY);
    this->isUsed = true;
    this->data.imageId = this->id;
    switch (this->id) {
        case BONUS_FLASH:
            this->object->getBody()->SetUserData((void*) IS_BONUS_FLASH);
            break;
        case BONUS_LIFE:
            this->object->getBody()->SetUserData((void*) IS_BONUS_LIFE);
            break;
    }
    this->hasDiedOrRevived = true;
    this->cronoStart = std::chrono::system_clock::now();
}

void Bonus::setUbicDyn(int id) {
    this->data.ubicDyn = id;
}

void Bonus::checkChanges() {
    b2Vec2 pos = this->object->getPosition();
    if (this->data.x == pos.x && this->data.y == pos.y) {
        this->isEqual = true;
    } else {
        this->isEqual = false;
    }
}

void Bonus::updateRotation() {

}

bool Bonus::getAreEquals() {
    if (this->hasDiedOrRevived) {
        this->dorsp++;
        if (this->dorsp > 4) {
            this->dorsp = 0;
            this->hasDiedOrRevived = false;
        }
        return false;
    }
    return true;
}

Object* Bonus::getObject() {
    return this->object;
}

b2Body* Bonus::getBody() {
    return this->object->getBody();
}

bool Bonus::hasToDisappear() {
    this->cronoNow = std::chrono::system_clock::now();
    int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
            (this->cronoNow - this->cronoStart).count();
    if (elapsed_seconds > this->timeToDisappear) {
        return true;
    } else {
        return false;
    }
}

Bonus::~Bonus() {
}
